package savet.gui.controllers;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.openswing.swing.mdi.client.ClientFacade;
import org.openswing.swing.mdi.client.MDIController;
import org.openswing.swing.mdi.client.MDIFrame;
import org.openswing.swing.mdi.java.ApplicationFunction;
import org.openswing.swing.permissions.client.LoginController;
import savet.Main;
import savet.SAVETFachada;
import savet.entidades.atributos.Legenda;
import savet.util.ListLegendas;
import savet.util.SauditaLog;
import savet.util.io.ArquivoUtil;

/**
 *
 * @author Adisson
 */
public class SAVETController implements MDIController, LoginController {
    public static final String SAUDITA_TITLE = "Saudita - Sistema Auditor de Atributos 1.0 [ALPHA]";

    private SAVETFachada fachada = new SAVETFachada();

    public void afterMDIcreation(MDIFrame frame) {
        System.out.println("Iniciado...");
        MDIFrame.getInstance().setIconImage(new ImageIcon("images/icone_48.png").getImage());
    }

    public String getAboutImage() {
        return null;
    }

    public String getAboutText() {
        return SAUDITA_TITLE;
    }

    public DefaultTreeModel getApplicationFunctions() {
        DefaultMutableTreeNode raiz = new DefaultMutableTreeNode();

        DefaultTreeModel model = new DefaultTreeModel(raiz);
        ApplicationFunction trabalhadores = new ApplicationFunction("Trabalhadores","", "Funções principais do sistema.");
        ApplicationFunction carregarXLS = new ApplicationFunction("Saudita p/ teste", "principal", "", "getGuiSavet");
        ApplicationFunction novoSaudita = new ApplicationFunction("Novo lote", "novoLote", "", "getNovoLote");
        ApplicationFunction salvarSaudita = new ApplicationFunction("Salvar lote", "salvarLote", "", "getSalvarLote");
        ApplicationFunction carregarSaudita = new ApplicationFunction("Carregar lote", "carregaLote", "", "getCarregarLote");
        
        ApplicationFunction ementario = new ApplicationFunction("Ementário","", "Módulo de gerenciamento de Ementas.");
        ApplicationFunction novaEmenta = new ApplicationFunction("Nova Ementa","nova ementa","","getEmentaForm");
        ApplicationFunction consultar = new ApplicationFunction("Consultar","pesquisa ementário","" ,"getEmentario");

        carregarXLS.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK));
        novoSaudita.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
        salvarSaudita.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
        carregarSaudita.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));

        trabalhadores.add(carregarXLS);
        trabalhadores.add(novoSaudita);
        trabalhadores.add(carregarSaudita);
        trabalhadores.add(salvarSaudita);
        

        ementario.add(novaEmenta);
        ementario.add(consultar);

        raiz.add(trabalhadores);
        raiz.add(ementario);
        return model;
    }

    public ClientFacade getClientFacade() {
        return fachada;
    }

    public int getExtendedState() {
        return JFrame.MAXIMIZED_BOTH;
    }

    public ArrayList getLanguages() {
        return new ArrayList();
    }

    public String getMDIFrameTitle() {
        return SAUDITA_TITLE;
    }

    public void stopApplication() {
        try {
            SauditaLog.adicionarInfoLog(SAVETController.class, "Encerrando aplicativo...");

            List<Legenda> legendas = ListLegendas.getInstance().getList();
            File fileLegendas = new File("resource/legendas.saud");

            ArquivoUtil.getInstance().salvarArquivo(fileLegendas, (Serializable) legendas);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.exit(0);
    }

    public boolean viewChangeLanguageInMenuBar() {
        return false;
    }

    public boolean viewFileMenu() {
        return false;
    }

    public boolean viewFunctionsInMenuBar() {
        return true;
    }

    public boolean viewFunctionsInTreePanel() {
        return false;
    }

    public JDialog viewLoginDialog(JFrame parentFrame) {
        return null;
    }

    public boolean viewLoginInMenuBar() {
        return false;
    }

    public boolean viewOpenedWindowIcons() {
        return true;
    }

    public boolean authenticateUser(Map loginInfo) throws Exception {
        return false;
    }

    public int getMaxAttempts() {
        return 3;
    }

    public void loginSuccessful(Map loginInfo) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
